home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / nfsmount / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-20  |  4.9 KB  |  204 lines

  1. /*
  2.  * NFS pseudo-filesystem server.
  3.  *
  4.  * This program is for a server process that acts as a gateway between
  5.  * a Sprite pseudo-filesystem and a remote NFS server.  This allows a
  6.  * NFS filesystem to be transparenty integrated into the Sprite distributed
  7.  * filesystem.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /sprite/src/cmds/nfsmount/RCS/main.c,v 1.11 91/10/20 12:37:45 mottsmth Exp $ SPRITE (Berkeley)";
  20. #endif not lint
  21.  
  22. #include "stdio.h"
  23. #include "option.h"
  24. #include "errno.h"
  25. #include "nfs.h"
  26. #include "strings.h"
  27. #include <syslog.h>
  28.  
  29. char myhostname[1024];
  30.  
  31. /*
  32.  * Command line options.
  33.  */
  34. char *prefix = "/nfs";
  35. char *host = "ginger.Berkeley.EDU";
  36. char *rfs = "/sprite";
  37. static int trace;
  38. int nfs_PdevWriteBehind = 1;
  39.  
  40. Option optionArray[] = {
  41.     {OPT_DOC, "", (Address)NIL, "nfssrv [-t] rhost:/path /prefix"},
  42.     {OPT_DOC, "", (Address)NIL, "(or use -p, -h, -r flags)"},
  43.     {OPT_STRING, "p", (Address)&prefix,
  44.         "Sprite prefix"},
  45.     {OPT_STRING, "h", (Address)&host,
  46.         "NFS host"},
  47.     {OPT_STRING, "r", (Address)&rfs,
  48.         "Remote filesystem name"},
  49.     {OPT_FALSE, "sync", (Address)&nfs_PdevWriteBehind,
  50.         "Disable write-behind"},
  51.     {OPT_TRUE, "t", (Address)&trace,
  52.         "Turn on tracing"},
  53.     {OPT_GENFUNC, "m", (Address)NfsRecordMountPointProc,
  54.         "NFS sub-mount point"},
  55. };
  56. int numOptions = sizeof(optionArray) / sizeof(Option);
  57.  
  58. struct timeval nfsTimeout = { 25, 0 };
  59.  
  60. void DoOpt();
  61.  
  62.  
  63. /*
  64.  *----------------------------------------------------------------------
  65.  *
  66.  * main --
  67.  *
  68.  *    Main program for NFS pseudo-filesystem server.  First the NFS
  69.  *    system is mounted, and then the pseudo-filesystem is established.
  70.  *    Lastly we drop into an Fs_Dispatch loop to handle pfs requests
  71.  *    comming from the Sprite kernel.
  72.  * 
  73.  * Results:
  74.  *    None.
  75.  *
  76.  * Side effects:
  77.  *    Opens the pseudo-filesystem.
  78.  *
  79.  *----------------------------------------------------------------------
  80.  */
  81.  
  82. main(argc, argv)
  83.     int argc;
  84.     char *argv[];
  85. {
  86.     NfsState *nfsPtr;
  87.     attrstat nfsAttr;
  88.     Fs_Attributes spriteAttr;
  89.     Fs_FileID rootID;
  90.  
  91.     argc = Opt_Parse(argc, argv, optionArray, numOptions, OPT_ALLOW_CLUSTERING);
  92.  
  93.     while (argc > 1) {
  94.     /*
  95.      * Look for "host:name [prefix]"
  96.      */
  97.     register char *colonPtr = index(argv[1], ':');
  98.     if (colonPtr != (char *)0) {
  99.         host = argv[1];
  100.         *colonPtr = '\0';
  101.         rfs = colonPtr + 1;
  102.         argc--;
  103.         argv++;
  104.     } else {
  105.         prefix = argv[1];
  106.         argc--;
  107.         argv++;
  108.     }
  109.     }
  110.  
  111.     gethostname(myhostname, sizeof(myhostname));
  112.  
  113.     nfsPtr = (NfsState *)malloc(sizeof(NfsState));
  114.     nfsPtr->host = host;
  115.     nfsPtr->prefix = prefix;
  116.     nfsPtr->nfsName = rfs;
  117.  
  118.     nfsPtr->mountClnt = Nfs_MountInitClient(host);
  119.     if (nfsPtr->mountClnt == (CLIENT *)NULL) {
  120.     exit(1);
  121.     }
  122.     nfsPtr->nfsClnt = Nfs_InitClient(host);
  123.     if (nfsPtr->nfsClnt == (CLIENT *)NULL) {
  124.     exit(1);
  125.     }
  126.  
  127.     nfsPtr->mountHandle = Nfs_Mount(nfsPtr->mountClnt, nfsPtr->nfsName);
  128.     if (nfsPtr->mountHandle == (nfs_fh *)NULL) {
  129.     exit(1);
  130.     }
  131.  
  132.     /*
  133.      * Test NFS access by getting the attributes of the root.  This is
  134.      * needed in order to set the rootID properly so it matches any
  135.      * future stat() calls by clients.
  136.      */
  137.     if (!NfsProbe(nfsPtr, trace, &nfsAttr)) {
  138.     Nfs_Unmount(nfsPtr->mountClnt, rfs);
  139.     exit(1);
  140.     }
  141.     NfsToSpriteAttr(&nfsAttr.attrstat_u.attributes, &spriteAttr);
  142.     rootID.serverID = spriteAttr.serverID;
  143.     rootID.type = TYPE_ROOT;
  144.     rootID.major = spriteAttr.domain;
  145.     rootID.minor = spriteAttr.fileNumber;
  146.     /*
  147.      * Set ourselves up as the server of the pseudo-file-system.  We'll
  148.      * see requests via the call-backs in nfsNameService.
  149.      */
  150.     if (trace) {
  151.     printf("RootID <%d,%d,%d,%d>\n", rootID.serverID, rootID.type,
  152.             rootID.major, rootID.minor);
  153.     }
  154.     nfsPtr->pfsToken = Pfs_Open(prefix, &rootID, &nfsNameService,
  155.     (ClientData)nfsPtr);
  156.     if (nfsPtr->pfsToken == (Pfs_Token)NULL)  {
  157.     if (trace) {
  158.             printf("%s\n", pfs_ErrorMsg);
  159.     }
  160.         syslog(LOG_ERR, "nfsmount: %s", pfs_ErrorMsg);
  161.     Nfs_Unmount(nfsPtr->mountClnt, rfs);
  162.     exit(1);
  163.     }
  164.  
  165.     if (trace) {
  166.     pdev_Trace = pfs_Trace = trace;
  167.     printf("NFS (traced): ");
  168.     } else {
  169.     printf("NFS: ");
  170.     }
  171.     printf("%s => %s:%s\n", prefix, host, rfs);
  172.  
  173.     if (!trace) {
  174.     Proc_Detach(0);
  175.     }
  176.  
  177.     while (1) {
  178.     Fs_Dispatch();
  179.     }
  180. }
  181.  
  182. /*
  183.  *----------------------------------------------------------------------
  184.  *
  185.  * BadProc --
  186.  *
  187.  *    Called by accident.
  188.  * 
  189.  * Results:
  190.  *    None.
  191.  *
  192.  * Side effects:
  193.  *    Enter the debugger.
  194.  *
  195.  *----------------------------------------------------------------------
  196.  */
  197. int
  198. BadProc()
  199. {
  200.     panic("Bad callback\n");
  201. }
  202.  
  203.  
  204.